package com.dinghui.ebookbag.module.middle;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.dinghui.ebookbag.R;
import com.dinghui.ebookbag.module.middle.fragment.AllDictionaryFragment;
import com.dinghui.ebookbag.module.middle.fragment.ComprehensiveKnowledgeFragment;
import com.dinghui.ebookbag.module.middle.fragment.FlipClassroomFragment;
import com.dinghui.ebookbag.module.middle.fragment.InterestChineseFragment;
import com.dinghui.ebookbag.module.middle.fragment.OutsideClassEnglishFragment;
import com.dinghui.ebookbag.module.middle.fragment.SyncClassroomFragment;
import com.dinghui.ebookbag.module.middle.fragment.ToolHelperFragment;
import com.dinghui.ebookbag.module.small.fragment.HomeFragment;

public class HighSchoolActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup hs_bottom_tabs;
    private RadioButton hs_rb_1;
    private FragmentManager fragmentManager;//管理fragment的类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_high_school);
        initView();
    }

    private void initView() {
        hs_bottom_tabs= (RadioGroup) findViewById(R.id.hs_bottom_tabs);
        hs_rb_1= (RadioButton) findViewById(R.id.hs_rb_1);
        //初始化FragmentManager
        fragmentManager=getSupportFragmentManager();
        //设置默认选中
        hs_rb_1.setChecked(true);
        hs_bottom_tabs.setOnCheckedChangeListener(this);
        //切换不同的fragment
        changeFragment(new SyncClassroomFragment(),false);


    }

    //改变选中按钮
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.hs_rb_1:
                changeFragment(new SyncClassroomFragment(),true);
                break;
            case R.id.hs_rb_2:
                changeFragment(new FlipClassroomFragment(),true);
                break;
            case R.id.hs_rb_3:
                changeFragment(new InterestChineseFragment(),true);
                break;
            case R.id.hs_rb_4:
                changeFragment(new OutsideClassEnglishFragment(),true);
                break;
            case R.id.hs_rb_5:
                changeFragment(new AllDictionaryFragment(),true);
                break;
            case R.id.hs_rb_6:
                changeFragment(new ComprehensiveKnowledgeFragment(),true);
                break;
            case R.id.hs_rb_7:
                changeFragment(new ToolHelperFragment(),true);
                break;
        }
    }

    //切换不同的fragment
    public void changeFragment(Fragment fragment, boolean isInit) {
        //开启事务
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.hs_content,fragment);
        if (!isInit) {
            transaction.addToBackStack(null);
        }
        transaction.commit();
    }


    // 返回键退出提示
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            AlertDialog.Builder builder = new AlertDialog.Builder(HighSchoolActivity.this);
            AlertDialog dialog = builder.create();
            dialog.setTitle("确定退出电子书包吗？");
            dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "返回", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {}
            });
            dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    System.exit(0);
                }
            });
            dialog.show();
        }
        return super.onKeyDown(keyCode, event);
    }





}